package com.example.eschool;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.eschool.son_fragment_home.BalanceFragment;
import com.example.eschool.son_fragment_home.LossFragment;
import com.example.eschool.son_fragment_home.NewsFragment;
import com.example.eschool.son_fragment_home.PaymencodeFragment;
import com.example.eschool.son_fragment_home.RelieveloseFragment;
import com.example.eschool.son_fragment_home.SchoolIntroduceFragment;
import com.example.eschool.son_fragment_home.UpdataPwdFragment;
import com.example.eschool.son_fragment_home.cashierdeskFragment;
import com.example.eschool.son_fragment_person.InvestFragment;
import com.example.eschool.son_fragment_person.Person_settingFragment;
import com.example.eschool.son_fragment_person.QuestionFragment;
import com.example.eschool.son_fragment_person.RecordFragment;
import com.example.eschool.son_fragment_person.SettingFragment;


public class SonActivity extends AppCompatActivity implements Button.OnClickListener{
    Button btn_return;
    TextView fragment_title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_son);

        // // 获取传递的参数，用于确定要加载的fragment
        String title = getIntent().getStringExtra("title");
        String fragmentName = getIntent().getStringExtra("fragment");

        initView();
        commitFragment(fragmentName, title);

        btn_return.setOnClickListener(this);
    }

    private void initView() {
        btn_return = findViewById(R.id.btn_return);
        fragment_title = findViewById(R.id.fragment_title);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_return:
                finish();
                break;
        }
    }

    public void commitFragment(String fragmentName, String title) {
        fragment_title.setText(title);
        switch (fragmentName) {

            //个人设置
            case "invest":
                replaceFragment(new InvestFragment());
                break;
            case "person_setting":
                replaceFragment(new Person_settingFragment());
                break;
            case "record":
                replaceFragment(new RecordFragment());
                break;
            case "question":
                replaceFragment(new QuestionFragment());
                break;
            case "setting":
                replaceFragment(new SettingFragment());
                break;


            //主页
            case "updataPwd":
                replaceFragment(new UpdataPwdFragment());
                break;
            case "tv_quick":
                replaceFragment(new InvestFragment());
                break;
            case "tv_recharge":
                replaceFragment(new InvestFragment());
                break;
            case "tv_balance":
                replaceFragment(new BalanceFragment());
                break;
            case "tv_payCode":
                replaceFragment(new PaymencodeFragment());
                break;
            case "tv_school_introduce":
                replaceFragment(new SchoolIntroduceFragment());
                break;
            case "tv_news":
                replaceFragment(new NewsFragment());
                break;
            case "tv_record":
                replaceFragment(new RecordFragment());
                break;
            case "tv_card_loss":
                replaceFragment(new LossFragment());
                break;
            case "tv_card_relieveloss":
                replaceFragment(new RelieveloseFragment());
                break;
            case "cashierdesk":
                replaceFragment(new cashierdeskFragment());
                break;

        }
    }

    public void replaceFragment(Fragment fragment){
        //fragment管理
        FragmentManager fragmentManager = getSupportFragmentManager();
        //创建
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        //替换
        transaction.replace(R.id.son_fragment,fragment);
        //加入栈，使其可以返回上一个fragment
        transaction.addToBackStack(null);
        //结束关闭
        transaction.commit();
    }
}